package drh.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.handlers.ScalarHandler;

import drh.bean.Product;
import drh.dao.BaseDao;
import drh.dao.ProductDao;
import handler.ProductResultSetHandler;

public class ProductDaoImpl extends BaseDao implements ProductDao {

	@Override
	public Integer deleteProductById(Integer id) {
		try {
			return query.update("delete from product where id=?",id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public Integer updateProduct(Product info) {
			try {
				return query.update("update product set productid=?,classification=?,originalprice=?,Presentprice=?,count=?,content=?,imgid=?,integral=?,time=?,isdelete=? where id=?",info.getProductid(),
						info.getClassification(),info.getOriginalprice(),info.getPresentprice(),info.getCount(),info.getContent(),info.getImgid(),info.getIntegral(),info.getTime(),info.getIsdelete(),info.getId());
			} catch (SQLException e) {
				e.printStackTrace();
			}
		return -1;
	}

	@Override
	public Integer addProduct(Product info) {
			try {
				return query.update("insert into product(productid,classification,originalprice,Presentprice,count,content,imgid,integral,time,isdelete) values(?,?,?,?,?,?,?,?,?,?)" ,info.getProductid(),
						info.getClassification(),info.getOriginalprice(),info.getPresentprice(),info.getCount(),info.getContent(),info.getImgid(),info.getIntegral(),info.getTime(),info.getIsdelete());
			} catch (SQLException e) {
				e.printStackTrace();
			}
		return null;
	}

	@Override
	public List<Product> GetProductPaging(Integer pageIndex, Integer pageSize) {
			String sql = "select * from product limit ?,?";
			try {
				return query.query(sql, new ProductResultSetHandler(),(pageIndex-1)*pageSize,pageSize);
			} catch (SQLException e) {
				e.printStackTrace(); 
			}
		return null;
	}

	public Integer getCount() {
		String sql = "select count(1) from product";
		Object query2;
		try {
			 query2 = query.query(sql, new ScalarHandler());
			return Integer.parseInt(query2.toString());
		} catch (SQLException e) {
	
			e.printStackTrace();
		}
		
		return null;
	}
}
